﻿using System;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace Uebung
{
    public class Bild
    {
        public String path;
        public Bitmap bmp;
        public bool aenderung = false;
        public bool zuschneiden = false;
        public bool saved = true;
        public Point origin;

        

        public Bild(String path)
        {
            this.path = path;
            this.bmp = new Bitmap(path);
        }
    }

    class BildEditor
    {
        private UI ui;
        public ArrayList imgs;
        public Bild activeBild;

        public BildEditor(UI ui)
        {
            this.ui = ui;
        }

        public ArrayList imagestoarray(string path)
        {
            string[] extensions = { "jpg", "png" };
            string[] files = Directory.GetFiles(path, "*.*", SearchOption.TopDirectoryOnly).Where(f => extensions.Contains(f.Split('.').Last().ToLower())).ToArray();
           

            imgs = new ArrayList();
            for (int i = 0; i < files.Length; ++i)
            {
                imgs.Add(new Bild(files[i]));
            }

            return imgs;
        }

        public Bitmap Crop( Rectangle selection)
        {
            
            if (!ValidateSelection(selection))
            {
                Debug.Print("selection has at least one dimension 0");
                return null;
            }
                Bitmap bmp = activeBild.bmp;

                // Check if it is a bitmap:
                if (bmp == null)
                    throw new ArgumentException("No valid bitmap");
                Debug.Print("selection:" + selection.ToString());
                Debug.Print(bmp.ToString());
                selection.Offset(activeBild.origin);
                Debug.Print("selection:" + selection.ToString());
                GraphicsUnit units = GraphicsUnit.Pixel;
                Debug.Print("bmp.GetBounds:" + bmp.GetBounds(ref units).ToString());
                // Crop the bmp:
                Bitmap cropBmp = bmp.Clone(selection, bmp.PixelFormat);

                // Release the resources:
                bmp.Dispose();
                activeBild.bmp = cropBmp;
                return cropBmp;
            
        }

        public Bitmap Fit2PictureBox(PictureBox picBox)
        {
            //Bitmap bmp = activeBild.bmp;
            Graphics g;

            // Scale:
            double scaleY = (double)activeBild.bmp.Width / picBox.Width;
            double scaleX = (double)activeBild.bmp.Height / picBox.Height;
            double scale = scaleY < scaleX ? scaleX : scaleY;

            // Create new bitmap:
            Bitmap bmp = new Bitmap(
                 Convert.ToInt32((double)activeBild.bmp.Width / scale),
                 Convert.ToInt32((double)activeBild.bmp.Height / scale));

            // Set resolution of the new bmp:
            bmp.SetResolution(
                activeBild.bmp.HorizontalResolution,
                activeBild.bmp.VerticalResolution);

            // Create graphics:
            g = Graphics.FromImage(activeBild.bmp);

            // Set interpolation mode:
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;

            // Draw the new bmp:
            g.DrawImage(
                bmp,
                new Rectangle(			// Ziel
                    0, 0,
                    activeBild.bmp.Width, activeBild.bmp.Height),
                new Rectangle(			// Quelle
                    0, 0,
                    activeBild.bmp.Width, activeBild.bmp.Height),
                GraphicsUnit.Pixel);

            // Release the resources of the graphics:
            g.Dispose();

            // Release the resources of the origin bmp:
            activeBild.bmp.Dispose();
            activeBild.bmp = bmp;
            return bmp;
        }

        public bool ValidateSelection(Rectangle selection)
        {
            // routine to validate the selection
            if (selection.Width == 0 || selection.Height == 0)
            {
                // if you get here a good rectangle was not created
                Debug.Print(selection.Width.ToString() + " x " + selection.Height.ToString());
                return false;
            }
            else
            {
                return true;
            }
        }

    }
}
